package com.da.landlord;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.googlecode.cswish.annotation.Rest;
import com.googlecode.cswish.model.PageInfo;
import com.googlecode.cswish.oosearch.Condition;
import com.googlecode.cswish.struts.spring.GenericService;

/**
 * 帐单 service
 * @author Peter.Z
 *
 */
@Service
public class DaChargeBillService {

	@Resource
	private GenericService genericService;
	
	
	/**
	 * 创建帐单 
	 * @param items
	 * @param room
	 * @param amountActual 实际收了多少钱
	 * @return true表示成功 false表示失败
	 */
	public void createChargeBill(List<DaChargeItem> items,DaRoom room,BigDecimal breakFee){
		
		 DaChargeBill chargeBill = new  DaChargeBill();
		 chargeBill.setRoom(room);
		 chargeBill.setChargeItems(items);
		 
		 BigDecimal amountRecv = new BigDecimal(0);
		 BigDecimal totalActual = new BigDecimal(0);
		 for (DaChargeItem daChargeItem : items) {
			 amountRecv = amountRecv.add(daChargeItem.getPriceRecv());
			 totalActual = totalActual.add(daChargeItem.getPriceActual());
		 }
		 
		 chargeBill.setAmountRecv(amountRecv);
		 chargeBill.setAmountActual(totalActual.subtract(breakFee));
		 
		 //余额
		 chargeBill.setBalance(chargeBill.getAmountActual().subtract(chargeBill.getAmountRecv()));
		 
		 chargeBill.setDateActual(new Date());
		 
		 //说明费用是哪一个合约下产生的
		 chargeBill.setAgreement(room.getAgreement());
		 
	     genericService.add(chargeBill);
	}
	
	
	/**
	 * 根据room，查找其下面的，所有帐单
	 * @param model
	 * @return
	 */
	public PageInfo<DaChargeBill> search(DaChargeBill model){
		DaRoom room = model.getRoom();
		//前端可见
		genericService.view(room);
		Condition condition = new Condition();
		condition.setDir("desc");
		condition.setSort("id");
		return genericService.searchByModel(model, condition);
	}
	
	/**
	 * 打印
	 * @param model
	 * @return
	 */
	@Rest(simpleServiceName = "anonymity", type = "free")
	public DaChargeBill print(DaChargeBill model){
		return genericService.load(model);
	}
	
	
}
